package oms.controllers;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oms.entities.User;
import oms.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	@Autowired
	private UserService	userService;

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String loginPage(Locale locale, Model model, HttpServletResponse response, @RequestParam(required=false) Integer error) {
		if(error!=null) {
			response.setHeader("message", "Incorrect login or password!");
		}
		return "forward:/pages/login.html";
	}

	@RequestMapping(value = "/admin/users/find", method = RequestMethod.GET)
	public @ResponseBody
	List<User> list(@RequestParam String field, @RequestParam String value,
			@RequestParam String match, @RequestParam int index, @RequestParam int size) {

		return userService.findUser(field, value, match, index, size);
	}

	

	//
	//
	// @RequestMapping("/admin")
	// public String setupForm(Map<String, Object> map){
	//
	// User user = new User();
	// map.put("user", user);
	// map.put("userList", userService.getAllUsers());
	//
	// return "user";
	// }
	//
	@RequestMapping(value = "/admin/users", method = RequestMethod.GET)
	public @ResponseBody
	List<User> list(@RequestParam("index") int index, @RequestParam("size") int size) {
		return userService.getAllUsers(index, size);
	}

	@RequestMapping(value = "/admin/login/{login}", method = RequestMethod.GET)
	public @ResponseBody
	User getByLogin(@PathVariable("login") String login) {
		return userService.getUserByLogin(login);
	}

	@RequestMapping(value = "/admin/mail/{mail}", method = RequestMethod.GET)
	public @ResponseBody
	User getByMail(@PathVariable("mail") String mail) {

		return userService.getUserByMail(mail);
	}

	@RequestMapping(value = "/admin/getCount", method = RequestMethod.GET)
	public @ResponseBody
	Integer getCount() {

		return userService.getCount();
	}

	@RequestMapping(value = "/admin/create", method = RequestMethod.POST)
	public String createUser(@RequestBody User user) {
		
		System.out.println(user);
		
		userService.createUser(user);

		return "redirect:/admin";
	}

	@RequestMapping(value = "/admin/update")
	public String updateUser(@ModelAttribute User user, BindingResult result) {

		userService.updateUser(user);

		return "redirect:/admin";
	}

	@RequestMapping(value = "/admin/delete", method = RequestMethod.GET)
	public String deleteUser(@RequestParam long id) {

		userService.deleteUser(id);
		return "redirect:/admin";
	}

	// @RequestMapping(value="/user.do", method=RequestMethod.POST)
	// public String doActions(@ModelAttribute User user, BindingResult result,
	// @RequestParam String action, Map<String, Object> map){
	//
	// User userResult = new User();
	//
	// switch(action.toLowerCase()) {
	//
	// case "create":
	// userService.createUser(user);
	// userResult = user;
	// break;
	//
	// case "update":
	// userService.updateUser(user);
	// userResult = user;
	// break;
	//
	// case "delete":
	// userService.deleteUser(user.getUserId());
	// userResult = new User();
	// break;
	//
	// case "search":
	// User searchedUser = userService.getUserById(user.getUserId());
	// userResult = searchedUser!=null ? searchedUser : new User();
	// break;
	// }
	//
	// map.put("user", user);
	// map.put("userList", userService.getAllUsers());
	// return "user";
	// }

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model, HttpServletRequest request) {

		if (request.isUserInRole("ROLE_ADMIN")) {
			return "redirect:/admin";
		}

		if (request.isUserInRole("ROLE_CUST")) {
			return "forward:/pages/customer.html";
		}

		if (request.isUserInRole("ROLE_MERCH")) {
			return "forward:/pages/items.html";
		}

		if (request.isUserInRole("ROLE_SUPER")) {
			return "forward:/pages/items.html";
		}

		return "redirect:/login";
	}

	@RequestMapping(value = "/customer", method = RequestMethod.GET)
	public String user(Locale locale, Model model) {

		return "customer";
	}

	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public String admin(Locale locale, Model model) {

		return "forward:/pages/admin.html";
	}

	@RequestMapping(value = "/items", method = RequestMethod.GET)
	public String items(Locale locale, Model model) {

		return "items";
	}

}
